package com.zyw.zhuangdian.service.impl;

import com.zyw.zhuangdian.constant.ServiceConstant;
import com.zyw.zhuangdian.dao.ArticleDao;
import com.zyw.zhuangdian.dao.FavouriteDao;
import com.zyw.zhuangdian.dao.ProductionDao;
import com.zyw.zhuangdian.enums.MsgCode;
import com.zyw.zhuangdian.pojo.Resp;
import com.zyw.zhuangdian.pojo.dto.PageDto;
import com.zyw.zhuangdian.pojo.in.FavouriteDtoIn;
import com.zyw.zhuangdian.pojo.out.ArticleDtoOut;
import com.zyw.zhuangdian.pojo.out.ProductionDtoOut;
import com.zyw.zhuangdian.property.MyProperties;
import com.zyw.zhuangdian.service.FavouriteService;
import com.zyw.zhuangdian.util.BeanConvertUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
public class FavouriteServiceImpl implements FavouriteService {
    private final Logger logger = LoggerFactory.getLogger(FavouriteServiceImpl.class);

    @Autowired
    private FavouriteDao FavouriteDao;

    @Autowired
    private ProductionDao productionDao;

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private MyProperties properties;

    @Override
    public Resp<Void> addFavourite(FavouriteDtoIn in) {
        Resp<Void> resp = new Resp<>();

        FavouriteDao.addFavourite(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> updateFavourite(FavouriteDtoIn in) {
        Resp<Void> resp = new Resp<>();

        FavouriteDao.updateFavourite(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<Void> deleteFavourite(FavouriteDtoIn in) {
        Resp<Void> resp = new Resp<>();
        FavouriteDao.deleteFavourite(BeanConvertUtil.beanCovertMap(in));
        return resp;
    }

    @Override
    public Resp<List<? extends PageDto>> listFavourites(FavouriteDtoIn in) {
        Resp<List<? extends PageDto>> resp = new Resp<>();
        if (in.getPage() != PageDto.DEFAULT_PAGE){
            if (in.getRow() == PageDto.DEFAULT_PAGE){
                in.setPage(PageDto.DEFAULT_PAGE);
            }else{
                in.setPage((in.getPage() - 1) * in.getRow());
            }
        }

        int count = 0;

        if(ServiceConstant.PRODUCTION.equals(in.getType())){
            count = FavouriteDao.listFavouritesCount(BeanConvertUtil.beanCovertMap(in));
            if (count > 0){
                List<Map> productions = FavouriteDao.listFavouriteProductions(BeanConvertUtil.beanCovertMap(in));
                List<ProductionDtoOut> dtoOuts = BeanConvertUtil.covertBeanList(productions,ProductionDtoOut.class);
                dtoOuts.forEach(item->item.setPictureList(Arrays.asList(item.getPictures().split(","))));
                resp.setData(dtoOuts);
            }
        }else if (ServiceConstant.ARTICLE.equals(in.getType())){
            count = FavouriteDao.listFavouritesCount(BeanConvertUtil.beanCovertMap(in));
            if (count > 0){
                List<Map> productions = FavouriteDao.listFavouriteArticles(BeanConvertUtil.beanCovertMap(in));
                List<ArticleDtoOut> dtoOuts = BeanConvertUtil.covertBeanList(productions,ArticleDtoOut.class);
                resp.setData(dtoOuts);
            }
        }else{
            resp.setCode(MsgCode.MSG_ILLEGAL_ARGUMENT.getCode());
            resp.setMsg(MsgCode.MSG_ILLEGAL_ARGUMENT.getMsg());
            return resp;
        }

        resp.setTotal(count);
        if (in.getPage() != PageDto.DEFAULT_PAGE) {
            resp.setRecords((int) Math.ceil((double) count / (double) in.getRow()));
            resp.setPage(in.getPage());
            resp.setRow(in.getRow());
        }
        return resp;
    }
}
